#include<iostream>
using namespace std;
const int N = 3e4 + 10;
int p[N];
int d[N];
int cnt[N];
int m;
int find(int x)
{
	int px = p[x];
	if (x != p[x]) {
		p[x] = find(p[x]);
		d[x] += d[px];
	}
	return p[x];
}
int main()
{
	cin >> m;
	for (int i = 1; i < N; i++) {
		p[i] = i;
		d[i] = 0;
		cnt[i] = 1;
	}
	char s[5];
	int a, b;
	while (m--) {
		//scanf("%s", s);
		cin >> s;
		if (s[0] == 'M') {
			cin >> a >> b;
			a = find(a);
			b = find(b);
			p[a] = b;
			d[a] = cnt[b];
			cnt[b] += cnt[a];
			//cout << " d[1]->" << d[1] << endl;
		}
		else {
			cin >> a; 
			int pa = find(a);
			cout << d[a] << endl;
		}
	}
	return 0;
}